# -*- coding:utf-8 -*-
class Solution:
    # 原码、反码和补码
    # https://blog.csdn.net/zl10086111/article/details/80907428
    def NumberOf1(self, n):
        l = 32 * [0]
        if n == 0:
            return 0
        elif n > 0:
            print("n>0")
            sub = 0
            while n != 0:
                print(n)
                l[sub] = n % 2
                ## 注意除法默认结果是float类型，要转换
                n = int(n / 2)
                sub += 1
            return sum(l)
        else:
            n = int(-n)
            sub = 0
            while n != 0:
                print(n)
                l[sub] = n % 2
                n = int(n / 2)
                sub += 1
            # 取反,使用%
            print(l)
            l = [(i+1) % 2  for i in l]
            print("取反后：", l)
            carry = 1
            new_l = list()
            for i in l:
                Sum = i + carry
                i = Sum % 2
                carry = int(Sum / 2)
                new_l.append(i)
            print(new_l)
            return sum(new_l)
        
        # write code here